﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Restaurant.Modules.REST.LocalService;
using Sector7.Restaurant.Modules.REST.Utility;
using Sector7.Restaurant.Biz;
using Sector7.Framework.Utility;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;

namespace Sector7.Restaurant.Modules.REST.Windows
{
    /// <summary>
    /// AddMenuLabelWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddMenuLabelWindow : AddModeWindowBase
    {
        public AddMenuLabelWindow()
        {
            InitializeComponent();
        }

        public RS_MenuItemLabel MenuItemLabel
        {
            get { return m_entity as RS_MenuItemLabel; }
            set { m_entity = value; }
        }

        protected override void InitializeForCreating()
        {
            MenuItemLabel = new RS_MenuItemLabel();
        }

        protected override void InitializeForUpdating()
        {
            btnContinue.Visibility = Visibility.Hidden;
        }

        protected override void AddData()
        {
            switch (m_crud)
            {
                case CrudType.Create:
                    MenuItemLabel.LabelId = Aide.LocalClient.Invoke<int>(
                        service => service.AddMenuItemLabel(MenuItemLabel));
                    break;
                case CrudType.Update:
                    EntityDelta delta = ClientUtil.CreateDelta<EntityDelta>(typeof(RS_MenuItemLabel));
                    delta.Conditions.Add("LabelId", MenuItemLabel.LabelId);
                    delta.DeltaProperties.Add("LabelName", MenuItemLabel.LabelName);
                    delta.DeltaProperties.Add("Image", MenuItemLabel.Image);

                    Aide.LocalClient.Invoke<int>(
                        service => service.SetMenuItemLabel(delta));
                    break;
                default:
                    break;
            }

            base.AddData();
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择图标";
            openFileDialog.Multiselect = false;
            openFileDialog.Filter = "PNG图像文件 (*.png)|*.png|GIF图像文件 (*.gif)|*.gif|JPEG图像文件 (*.jpg, *.jpeg)|*.jpg;*.jpeg|所有文件|*.*";

            bool? result = openFileDialog.ShowDialog();
            if (result == true)
            {
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = File.OpenRead(openFileDialog.FileName) as Stream;
                bitmapImage.EndInit();
                
                imgPreview.Source = bitmapImage;

                byte[] data = new byte[bitmapImage.StreamSource.Length];
                bitmapImage.StreamSource.Seek(0, SeekOrigin.Begin);
                bitmapImage.StreamSource.Read(data, 0, data.Length);

                MenuItemLabel.Image = data;

                txbFileName.Text = openFileDialog.FileName;
            }
        }
    }
}
